import React, { Component, PropTypes } from 'react';
import { ContextMenu, MenuItem, connect } from 'react-contextmenu';
import { noop } from 'lodash';

import { NextActionOptions } from '../actions/path-input-bar-actions.js';

@connect
export default class APIMockStateContextMenu extends Component {
    static propTypes = {
        /**
         * 状态文字
         * @type {string}
         */
        text: PropTypes.string,

        /**
         * 重新启动 Mock 服务
         * @type {Function}
         */
        reloadMockServer: PropTypes.func,

        /**
         * push code to coding.net
         * @type {Function}
         */
        pushCodeServer: PropTypes.func,

        /**
         * 设置服务器时间
         * @type {Function}
         */
        setTimeServer: PropTypes.func,

        /**
         * 消息提示
         * @type {Function}
         */
        showNotification: PropTypes.func
    };

    componentDidUpdate(prevProps) {
        const { showNotification, text } = this.props;

        // Decide whether to show a notification or not after Checking updates
        if (text === 'Online' && text !== prevProps.text) {
            showNotification({
                text: 'Mock Server is now online.',
                type: 'success',
                duration: 1000
            });
        }

        if (text === 'Time' && text !== prevProps.text) {
            showNotification({
                text: 'Server time is set.',
                type: 'success',
                duration: 1000
            });
        }
    }

    render() {
        const { text, reloadMockServer, pushCodeServer, setTimeServer } = this.props;

        return (
            <ContextMenu identifier="api-mock-state-context-menu">
                <MenuItem disabled onClick={noop}>Current: {text}</MenuItem>
                <MenuItem onClick={reloadMockServer}>Restart</MenuItem>
                <MenuItem onClick={pushCodeServer}>Push Code</MenuItem>
                <MenuItem onClick={setTimeServer}>Set The Server Time</MenuItem>
            </ContextMenu>
        );
    }
}